﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.DataProvider;
using CuaHangMayTinh.Utilities;
using System.Data;

namespace CuaHangMayTinh.DataAccessLayer
{
    public class QuyenDA
    {
        public QuyenDA()
        {

        }

        /// <summary>
        /// Hàm thêm 1 Quyền mới
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Insert(QuyenEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("Ten", entity.Ten);
                return (int)DBFactory.Database.ExecuteNonQuery("Quyen_Insert", pb.Parameters);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm Cập nhật 1 Quyền
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpDate(QuyenEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("IDQuyen", entity.IDQuyen);
                pb.AddParameter("Ten", entity.Ten);

                int numRowEffected = DBFactory.Database.ExecuteNonQuery("Quyen_Update", pb.Parameters);
                return (numRowEffected > 0);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm tìm tất cả thông tin  QUyền
        /// </summary>
        /// <returns></returns>
        public DataTable Find_All()
        {
            try
            {
                return DBFactory.Database.FillDataTable("SELECT * FROM Quyen");
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm tìm thông tin quyền bằng ID của SubGroupUser
        /// </summary>
        /// <param name="IDSubGroupUser">ID của 1 SubGroupUser</param>
        /// <returns>DataTable</returns>
        public DataTable Find_IDSubGroupUser(int IDSubGroupUser)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("IDSubGroupUser", IDSubGroupUser);

                dt = DBFactory.Database.FillDataTable("Quyen_Find_IDSubGroupUser", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm tìm thông tin quyền không có của SubGroupUser bằng IDSubGroupUser
        /// </summary>
        /// <param name="IDSubGroupUser"></param>
        /// <returns></returns>
        public DataTable FindNot_IDSubGroupUser(int IDSubGroupUser)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("IDSubGroupUser", IDSubGroupUser);

                dt = DBFactory.Database.FillDataTable("Quyen_FindNot_IDSubGroupUser", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm tìm thông tin quyền bằng ID của 1 Tài khoản
        /// </summary>
        /// <param name="IDTaiKhoan">ID tài khoản</param>
        /// <returns></returns>
        public DataTable Find_IDTaiKhoan(int IDTaiKhoan)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("IDTaiKhoan", IDTaiKhoan);

                dt = DBFactory.Database.FillDataTable("Quyen_Find_IDTaiKhoan", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm chuyển 1 DataTable thành 1 list QuyenEntity
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<QuyenEntity> ConvertToList(DataTable dt)
        {
            List<QuyenEntity> list = new List<QuyenEntity>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int id = (int)dt.Rows[i][0];
                string ten = dt.Rows[i][1].ToString();

                QuyenEntity a = new QuyenEntity(id, ten);
                list.Add(a);
            }
            return list;
        }
    }
}
